#include "infobox.h"

InfoBox::InfoBox(int dir, QString t, QGraphicsItem *parent) :
    QGraphicsWidget(parent)
{
    direction = dir;

    textToDraw = t;

    textItem = new QGraphicsTextItem( this );
    textItem->setPlainText( textToDraw);
    textSize = 14;
    textWidth = 100;//125; //default value
    font = new QFont("Helvetica [Cronyx]", textSize);
    font->setPixelSize(textSize);
    QColor textColor( 255.0, 255.0, 255.0, 255.0 );
    textItem->setDefaultTextColor ( textColor );
    needToComputeTextBoundaries = true;

//    if( direction == 0 )//TOP
//    {
//        setupTop();
//    }
//    else if( direction == 2 ) //BOTTOM
//    {
//        setupBottom();
//    }

    setCacheMode(QGraphicsItem::ItemCoordinateCache);

    //textItem->setVisible( false );
}

InfoBox::~InfoBox()
{
    if( textItem != NULL )
        delete textItem;
    if( font != NULL )
        delete font;
}

void InfoBox::setupTop()
{
    int linesOfText = textToDraw.count("\n");

    float left = (float)-textWidth/2.0-20.0;
    float right = (float)textWidth/2.0+20.0;
    float top = -1.0*textSize*(linesOfText+2.0);//-1.0*textSize*(linesOfText*2.0+1.0); //-50.0;
    float bottom = 0.0;
    boundRect.setCoords(left, top, right, bottom );//-50.0, -50.0, 50.0, 0.0);
    textRect.setCoords(left, top, right, bottom-5.0 );//top+5.0, right, bottom-5.0 );//-50.0, -45.0, 50.0, -5.0 );

    textItem->setTextWidth( right-left );
    QTextBlockFormat format;
    format.setAlignment(Qt::AlignCenter);
    QTextCursor cursor = textItem->textCursor();
    cursor.select(QTextCursor::Document);
    cursor.mergeBlockFormat(format);
    cursor.clearSelection();
    textItem->setTextCursor(cursor);
    textItem->setPos( left, top);//+.5*textSize );//+textSize*1.5 );

    trianglePt1.setX( 0.0 );
    trianglePt1.setY( 0.0 );

    trianglePt2.setX( -5.0 );
    trianglePt2.setY( -5.1 );

    trianglePt3.setX( 5.0 );
    trianglePt3.setY( -5.1 );

}

void InfoBox::setupBottom()
{
    int linesOfText = textToDraw.count("\n");

    float left = (float)-textWidth/2.0-20.0;
    float right = (float)textWidth/2.0+20.0;
    float top = textSize*(linesOfText+2.0);//-1.0*textSize*(linesOfText*2.0+1.0); //-50.0;
    float bottom = 0.0;
    boundRect.setCoords(left, bottom, right, top );
    textRect.setCoords(left, bottom+5.0, right, top );

//    qDebug() << "accessing text item " << endl;
    textItem->setTextWidth( right-left );
    QTextBlockFormat format;
    format.setAlignment(Qt::AlignCenter);
    QTextCursor cursor = textItem->textCursor();
    cursor.select(QTextCursor::Document);
    cursor.mergeBlockFormat(format);
    cursor.clearSelection();
    textItem->setTextCursor(cursor);
    textItem->setPos( left, bottom+5.0 );


    trianglePt1.setX( 0.0 );
    trianglePt1.setY( 0.0 );

    trianglePt2.setX( -5.0 );
    trianglePt2.setY( 5.1 );

    trianglePt3.setX( 5.0 );
    trianglePt3.setY( 5.1 );
}

void InfoBox::setText( QString t )
{
    textToDraw = t;
    needToComputeTextBoundaries = true;

}

void InfoBox::addLineOfText( QString t )
{
    textToDraw.append("\n").append( t );
    textItem->setPlainText( textToDraw );

    needToComputeTextBoundaries = true;

    //update();
}

void InfoBox::changeDirection(int dir )
{
    direction = dir;
    if( direction == 0 )
        setupTop();
    else if( direction == 2 )
        setupBottom();
}



//-------------------PAINT FUNCTIONS ------------------
QRectF InfoBox::boundingRect() const
{
    return boundRect;
}

QPainterPath InfoBox::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void InfoBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    if( needToComputeTextBoundaries )
    {
//        qDebug() << "computing text boundaries " << endl;
//        //find out text width, so it doesn't go out of bounds
//        font->setPixelSize(textSize);
//        painter->setFont( *font );
        QFontMetrics fm = painter->fontMetrics();

        QStringList list = textItem->toPlainText().split("\n"); //split on new lines
        int maxChar = 0;
        int idx = 0;
        for(int i = 0; i < list.size(); i++)
        {
            if( list[i].size() > maxChar )
            {
                idx = i;
                maxChar = list[i].size();
            }
        }

////        textWidth = 0;
////        for(int i = 0; i < list.size(); i++ )
////        {
////            qDebug() << "           text: " << list[i] << endl;
////            int thisTextWidth = fm.width(list[i]);
////            qDebug() << "               this text width: " << thisTextWidth << endl;
////            qDebug() << "               text width: " << textWidth << endl;
////            qDebug() << "               thisTextWidth > textWidth " << (thisTextWidth > textWidth) << endl;
////            if( thisTextWidth > textWidth )
////                textWidth = thisTextWidth; //store new textWidth if this text is wider than other lines of text
////        }

        textWidth = fm.width(list[idx]);
        textHeight = fm.height();
//        QFontMetrics fm(font);
//        textWidth = fm.width(textToDraw);
//        textHeight = fm.height(textToDraw);

        if( direction == 0 )
            setupTop();
        if( direction == 2 )
            setupBottom();

        needToComputeTextBoundaries = false;
        update();
    }

    if( textRect.width() < 10 )
        return;
    QColor labelBackground( 0, 0, 0, 200);//125.0, 125.0, 125.0, 200.0);

    painter->setBrush( labelBackground );
    painter->setPen( Qt::NoPen );
    painter->drawRect( textRect );

    QPolygonF poly;
    poly << trianglePt1 << trianglePt2 << trianglePt3;
    painter->drawPolygon(poly);

//    painter->setPen( Qt::white);
//    font->setPixelSize(textSize);
//    painter->setFont( *font );
//    painter->drawText(textRect, Qt::AlignCenter, textToDraw);
}
